<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.Map"%>
<%@ page import="com.alipay.util.*"%>
<%@ page import="com.alipay.config.*"%>
<%@ page import="com.webbei.shoppingCar.action.MyAlipayApi"%>
<html>
  <head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>支付宝页面跳转同步通知页面</title>
  </head>
  <body>
<%
	Map<String,String> params = new HashMap<String,String>();
	Map requestParams = request.getParameterMap();
	for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
		String name = (String) iter.next();
		String[] values = (String[]) requestParams.get(name);
		String valueStr = "";
		for (int i = 0; i < values.length; i++) {
			valueStr = (i == values.length - 1) ? valueStr + values[i]:valueStr + values[i] + ",";
		}
		valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
		params.put(name, valueStr);
	}
	String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
	String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
	String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
	String logistics_fee = new String(request.getParameter("logistics_fee").getBytes("ISO-8859-1"),"UTF-8");
	String refund_status = new String(request.getParameter("refund_status").getBytes("ISO-8859-1"),"UTF-8");

	boolean verify_result = AlipayNotify.verify(params);
	if(verify_result){//验证成功
		if(trade_status.equals("WAIT_SELLER_SEND_GOODS")){
			System.out.println("买家已付款,等待卖家发货");
			MyAlipayApi.saveOrderFromCustomForAlipay(Long.parseLong(out_trade_no),Double.parseDouble(logistics_fee),Long.parseLong(trade_no));
			out.println("购买成功。<script language='javascript' type='text/javascript'> setTimeout('javascript:location.href=\"showProducts.action\"', 1000); </script>");
		}else if(trade_status.equals("WAIT_BUYER_CONFIRM_GOODS")){
			System.out.println("卖家已发货,等待买家确认收货");
			MyAlipayApi.deliverSuccessForAlipay(Long.parseLong(trade_no));
		}else if(trade_status.equals("TRADE_FINISHED")){
			System.out.println("交易成功");
			MyAlipayApi.tradeSuccessForAlipay(Long.parseLong(trade_no));
		}

		if(refund_status.equals("WAIT_SELLER_AGREE")){
			System.out.println("买家申请退款,等待卖家确认");
			MyAlipayApi.refundApplyForAlipay(Long.parseLong(trade_no));
		}else if(refund_status.equals("REFUND_SUCCESS")){
			System.out.println("退款成功");
			MyAlipayApi.refundSuccessForAlipay(Long.parseLong(trade_no));
		}else if(refund_status.equals("REFUND_CLOSED")){
			System.out.println("退款关闭");
			MyAlipayApi.refundCloseForAlipay(Long.parseLong(trade_no),trade_status);
		}

	}else{
		out.println("系统错误！！请联系客服！！");
	}
%>
  </body>
</html>